﻿using DatabaseAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Liguo.Administration
{
    public partial class UserInformation : System.Web.UI.Page
    {
        SqlIt sql = new SqlIt();
        //DataSet类承接查询结果
        DataSet ds=new DataSet();
        
           protected void Page_Load(object sender, EventArgs e)
           {
                 ds = sql.SelectAll("UserInfo");
                    if (!IsPostBack)
                   {
                          bind(ds);
                    }
            }

              protected void search_Click(object sender, EventArgs e)
              {
                  string sqlstr = "";
                  int flag = 0;
                  if (username.Checked) {
                      sqlstr += "UserName='" + InputUser.Text+"'";
                      flag = 1;
                  }
                  if (mailbox.Checked) {
                      if (flag == 0)
                      {
                          sqlstr += "MailBox='" + InputMail.Text + "'";
                      }
                      else 
                      {
                          sqlstr += "and MailBox='" + InputMail.Text + "'";
                      }
                  }
                  if (sqlstr == "")
                  {
                      ds = sql.SelectAll("UserInfo");
                  }
                  else 
                  {
                     ds = sql.SpecialItem("UserInfo", sqlstr);
                  }
                  bind(ds);
              }
   //绑定          
              public void bind(DataSet ds)
               {
                  GridView1.DataSource = ds;
                  GridView1.DataKeyNames = new string[] { "ID" };//主键
                  GridView1.DataBind();
                  DropDownList ddl;
                  for (int i = 0; i < GridView1.Rows.Count; i++)
                  {
                      DataTable dt = new DataTable();
                      DataRow drv = dt.NewRow();
                      drv = ds.Tables[0].Rows[i];
                      if (Convert.ToBoolean(drv["Status"]) == false)
                      {
                          ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                          //ddl.SelectedIndex = 0;
                          ddl.Items.FindByValue("0").Selected = true;
                      }
                      if (Convert.ToBoolean(drv["Status"]) == true)
                      {
                          ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
                          //ddl.SelectedIndex = 1;
                          ddl.Items.FindByValue("1").Selected = true;
                      }
                  }

              }

              protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
              {
                  //Boolean judge=false;
                 // Response.Write("<script>confirm('确认要改变？')</script>");
                  DropDownList DropDownList1 = sender as DropDownList;
                  int index = (DropDownList1.NamingContainer as GridViewRow).RowIndex;//命名容器
                  string id = GridView1.DataKeys[index].Value.ToString();//设定的datakey属性
                  string selectedv = DropDownList1.SelectedValue;
                  if (selectedv.Equals("0")) 
                  {
                      //Response.Write("<script>confirm('确认要禁用？')</script>");
                      ds.Tables[0].Rows[index]["Status"] = selectedv;
                      Dictionary<string, string> ud = new Dictionary<string, string>();
                      ud["Status"] = "0";
                      string constriant;
                      constriant = "ID='" + id + "'";
                      sql.UpdateMess("UserInfo", ud, constriant);
                      Response.Write("<script>alert('该用户已禁用！')</script>");
                  }
                  else if (selectedv.Equals("1"))
                  {
                     // Response.Write("<script>confirm('确认要启用？')</script>");
                      ds.Tables[0].Rows[index]["Status"] = selectedv;
                      Dictionary<string, string> ud = new Dictionary<string, string>();
                      ud["Status"] = "1";
                      string constriant;
                      constriant = "ID='" + id + "'";
                      sql.UpdateMess("UserInfo", ud, constriant);
                      Response.Write("<script>alert('该用户已启用！')</script>");
                  }
                  bind(ds);
              }

     }
}